package UIComponents
{
	import Classes.GameUtils.MyTipSprite;
	import Classes.GameUtils.ToolTip_Sprite;
	import Classes.staticinfo.Language;
	
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	import flash.text.TextField;
	
	public class MakingFrameItemPanel extends Sprite
	{
		private var price:TextField;
		private var makingNum:TextField;
		private var ic:IconContainer;
		private var buyButton:MyButton;
		public var mfmb:DisplayObject;
		private var moneydc:DisplayObjectContainer;
		public function MakingFrameItemPanel()
		{
			init();
		}
		
		private function init():void
		{
			this.price=this.getChildByName("txt_itemValue") as TextField;
			this.makingNum=this.getChildByName("makingNum") as TextField;
			this._makingNumNormalColor	=	this.makingNum.textColor;
			this.price.mouseEnabled=this.makingNum.mouseEnabled=false;
			this.ic=this.getChildByName("itemSp") as IconContainer;
			this.buyButton=this.getChildByName("buyBtn") as MyButton;
			this.mfmb=this.getChildByName("noItemSp");
			
			this.moneydc=this.getChildByName("moneySp") as DisplayObjectContainer;
			this.moneydc.getChildByName("icon_jb").visible=false;
			if(FlatInfo.isOPenQQVip)
			{
				(this.moneydc.getChildByName("icon_ob") as DisplayObjectContainer).getChildByName("qdian").visible=true;
				(this.moneydc.getChildByName("icon_ob") as DisplayObjectContainer).getChildByName("obIcon").visible=false;
			}else{
				(this.moneydc.getChildByName("icon_ob") as DisplayObjectContainer).getChildByName("qdian").visible=false;
				(this.moneydc.getChildByName("icon_ob") as DisplayObjectContainer).getChildByName("obIcon").visible=true;
			}
			
			var matrix:Array = new Array();
			
			matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // red
			
			matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // green
			
			matrix = matrix.concat([0.3086, 0.6094, 0.082, 0, 0]); // blue
			
			matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha
			
			var gray:ColorMatrixFilter = new ColorMatrixFilter(matrix);
			
			var filtersArray:Array = new Array(gray);
			this.price.filters=filtersArray;
			
			this.buyButton.filters=filtersArray;
			
			initLister();
		}
		private function initLister():void
		{
			this.buyButton.addEventListener(ComponentEvent.BUTTON_DOWN,buyItemFun);
		}
		
		public function buyItemFun(e:ComponentEvent):void
		{
			//点击购买按钮触发的逻辑
			this.shopObj.sendBuyCommand();
		}
		
		private var _makingNumNormalColor:uint;
		private var shopObj:IGD_ShopItem;
		public function setFrameData(obj:Object):Boolean
		{
			this.buyButton.filters=null;
			this.price.filters=null;
			this.buyButton.enabled=true;
			var tmpFlag:Boolean=true;
			this.reset();
			this.mfmb.visible=false;
			this.makingNum.text=obj.needItemNum+"/"+obj.targetValue;
			this.makingNum.textColor	=	( obj.needItemNum < obj.targetValue? 0xff0000:this._makingNumNormalColor);
			
			this.setItemPic(obj.needItemIcon,obj.needItemName+"："+obj.needItemDes);
			this.shopObj=obj.IDg_shop;
			this.price.text=String(this.shopObj.price);
			if(Number(obj.needItemNum)<1)
			{
				tmpFlag=false;
			}
			
			this.setBuyTip();
			
			return tmpFlag;
		}
		
		/**
		 * 物品图片  加热感
		 * @param value
		 */	
		public function setItemPic(value:DisplayObject,itemName:String=""):void
		{
			if(null==value){
				this.ic.addPic(null);
				return;	
			}else{
				value.width=59;
				value.height=63;
				this.ic.addPic(value,itemName);
			}
		}
		
		public function reset():void
		{
			this.mfmb.visible=true;
			this.price.text="0";
			this.makingNum.text="0/0";
			this.ic.addPic(null,"");
			this.setItemPic(null);
		}
		
		private function setBuyTip():void
		{
			
			//购买按钮
			if(null!=this.shopObj)
			{
				var moenytype:String=FlatInfo.isOPenQQVip?Language.Qdian:Language.ob;
				
				var upBuyTips:String=Language.upBuyBtnTips;
				while(upBuyTips.indexOf("@needItemName")!=-1)
				{
					upBuyTips=upBuyTips.replace("@needItemName",shopObj.itemName);
				}
				while(upBuyTips.indexOf("@pil")!=-1)
				{
					upBuyTips=upBuyTips.replace("@pil",1);
				}
				upBuyTips=upBuyTips.replace("@totalPrice",shopObj.price);
				upBuyTips=upBuyTips.replace("@moenytype",moenytype);
				upBuyTips=upBuyTips.replace("@itemNum",shopObj.perNum);
				
				
				ToolTip_Sprite.getInstance().setTip(this.moneydc,new MyTipSprite(upBuyTips),ToolTip_Sprite.DIRECTION_LEFT);
				ToolTip_Sprite.getInstance().setTip(this.buyButton,new MyTipSprite(upBuyTips),ToolTip_Sprite.DIRECTION_LEFT);
			}
		}
	}
}